#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 18 16:43:52 2019

@author: jili
"""
from pymongo import MongoClient as mc
client_tick = mc("mongodb://ant:ant@localhost:27777/tick_f")
tick=client_tick["tick_f"]
tick_cu={}
def gettick_cu(objid):
    if (objid not in tick_cu.keys()):
        if (objid in tick.list_collection_names()):
            name="c_"+objid
            tick_cu[objid]=tick[name]
        else:
            tick_cu[objid]=tick[name]
            tick_cu[objid].create_index([("timekey", 1)], unique=True, background=True)
    return tick_cu[objid]
tick["c_rb1910"].create_index([("timekey", 1)], unique=True, background=True)
def insert_one(t):
    # 可以使用insert直接一次性向mongoDB插入整个列表，也可以插入单条记录，但是'_id'重复会报错
    try:
        #cu = gettick_cu(objid)
        tick["c_rb1910"].insert_one(t)
    except Exception as e:
        print(e)
cur= tick["rb1910"].find({}).batch_size(5000)
for i in cur:
    insert_one(i)
#tick_cu["rb1910"].rename("rb1910")